package com.lw.main;

import com.lw.util.Constant;
import com.lw.util.GameUtil;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 前景 相对于背景而言叫前景
 */
public class GameFrontGround {

    private static final int CLOUD_COUNT = Constant.CLOUD_IMG.length;//云彩个数，因为两个云图片

    //存放云彩的容器
    private List<Cloud> clouds;

    private static final int CLOUD_SPEED = 10;//云彩飞行速度

    //使用的图片资源
    private BufferedImage[] imgs;

    private Random random = new Random();

    public GameFrontGround() {
        clouds = new ArrayList<>();
        imgs = new BufferedImage[CLOUD_COUNT];

        //添加云彩图片
        for (int i = 0; i < CLOUD_COUNT; i++) {
            imgs[i] = GameUtil.loadBufferedImage(Constant.CLOUD_IMG[i]);
        }

        initCloud(15);
    }

    /**
     * 产生云朵
     * @param num
     */
    private void initCloud(int num){
        for (int i = 0; i < num; i++) {
            Cloud cloud = new Cloud(imgs[random.nextInt(CLOUD_COUNT)], CLOUD_SPEED, Constant.WIDTH+ random.nextInt(Constant.WIDTH), random.nextInt(150));
            clouds.add(cloud);
        }
    }

    /**
     * 绘制云彩
     * @param g
     */
    public void draw(Graphics g){
        this.logic();//用于产生云彩

        for (int i = 0; i < clouds.size(); i++) {
            clouds.get(i).draw(g);
        }
    }

    /**
     * 用于控制云彩个数
     */
    public void logic(){
//        if(500*Math.random()<50){//[0,500)
//            Cloud cloud = new Cloud(imgs[random.nextInt(CLOUD_COUNT)], CLOUD_SPEED, Constant.WIDTH, random.nextInt(150));
//            clouds.add(cloud);
//            System.out.println("产生云");
//        }
//        for (int i = 0; i < clouds.size(); i++) {
//            Cloud cloud = clouds.get(i);
//            if(cloud.isOutFrame()){//超出屏幕移除
//                clouds.remove(i);
//                i--;
//                System.out.println("移除云");
//            }
//        }
        for (int i = 0; i < clouds.size(); i++) {
            Cloud cloud = clouds.get(i);
            if(cloud.isOutFrame()){
                cloud.changeLocation(Constant.WIDTH+ random.nextInt(Constant.WIDTH), random.nextInt(150));
            }

        }
    }
}
